#ifndef CUESHEET_H_
#define CUESHEET_H_

#include <nds.h>

struct CuesheetTrackIndex
{
	u64 offset; /* Offset in samples, relative to the track offset, of the index point.
					For CD-DA, the offset must be evenly divisible by 588 samples
					(588 samples = 44100 samples/sec * 1/75th of a sec). Note that
					the offset is from the beginning of the track, not the beginning
					of the audio data. */
	u8 index;   /* The index point number. For CD-DA, an index number of 0 corresponds
					to the track pre-gap. The first index in a track must have a number
					of 0 or 1, and subsequently, index numbers must increase by 1.
					Index numbers must be unique within a track. */
};

struct CuesheetTrack
{
	u8 offset;       /* Track offset in samples, relative to the beginning of the FLAC audio stream.
						It is the offset to the first index point of the track. (Note how this differs
						from CD-DA, where the track's offset in the TOC is that of the track's INDEX 01
						even if there is an INDEX 00.) For CD-DA, the offset must be evenly divisible
						by 588 samples (588 samples = 44100 samples/sec * 1/75th of a sec). */
	u8 number;       /* Track number. A track number of 0 is not allowed to avoid conflicting with
						the CD-DA spec, which reserves this for the lead-in. For CD-DA the number
						must be 1-99, or 170 for the lead-out; for non-CD-DA, the track number
						must for 255 for the lead-out. It is not required but encouraged to start
						with track 1 and increase sequentially. Track numbers must be unique within
						a CUESHEET. */
	char iscr[12];      /* Track ISRC. This is a 12-digit alphanumeric code
						A value of 12 ASCII NUL characters may be used to denote absence of an ISRC. */
	bool is_audio;   /* The track type: 0(true) for audio, 1(false) for non-audio.
						This corresponds to the CD-DA Q-channel control bit 3. */
	bool emphasis;   /* The pre-emphasis flag: 0(false) for no pre-emphasis, 1(true) for pre-emphasis.
						This corresponds to the CD-DA Q-channel control bit 5. */
	u8 no_indexes; // number of indexes
	CuesheetTrackIndex *indexes;
};

class Cuesheet
{
private:
	char catalog[128];     /* Media catalog number, in ASCII printable characters 0x20-0x7e.
							In general, the media catalog number may be 0 to 128 bytes long;
							any unused characters should be right-padded with NUL characters.
							For CD-DA, this is a thirteen digit number, followed by 115 NUL bytes. */
	u64  samples;          /* The number of lead-in samples.
							This field has meaning only for CD-DA cuesheets; for other uses it should be 0.
							For CD-DA, the lead-in is the TRACK 00 area where the table of contents is stored;
							more precisely, it is the number of samples from the first sample of the media
							to the first sample of the first index point of the first track.
							According to the Red Book, the lead-in must be silence and CD grabbing software
							does not usually store it; additionally, the lead-in must be at least two seconds
							but may be longer. For these reasons the lead-in length is stored here so that
							the absolute position of the first track can be computed. Note that the lead-in
							stored here is the number of samples up to the first index point of the first track,
							not necessarily to INDEX 01 of the first track; even the first track may have INDEX 00 data. */
	bool is_cd;              // true(1) if the CUESHEET corresponds to a Compact Disc, else false(0).
	u8 no_tracks;
	CuesheetTrack *tracks; // table of cuesheet track

public:
	void init(char* catNo, u64 sampls, bool cd, u8 tracks);
	void add(CuesheetTrack track);
	CuesheetTrack getTrack(u8 no);
	~Cuesheet();
};

#endif /* CUESHEET_H */
